package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.persistence.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountService {
    @Autowired
    private AccountMapper accountMapper;

    public Account getAccount(String username) {
        return accountMapper.getAccountByUsername(username);
    }

    public Account getAccount(String username, String password) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return accountMapper.getAccountByUsernameAndPassword(account);
    }
    public Account getAccountbyPhonenumber(String phoneNum){
        return accountMapper.getAccountByPhonenum(phoneNum);
    }

    @Transactional
    public void insertAccount(Account account) {
        accountMapper.insertAccount(account);
        accountMapper.insertProfile(account);
        accountMapper.insertSignon(account);
    }

    @Transactional
    public void updateAccount(Account account) {
        accountMapper.updateAccount(account);
        accountMapper.updateProfile(account);

        if (account.getPassword() != null && account.getPassword().length() > 0) {
            accountMapper.updateSignon(account);
        }
    }
    public boolean usernameIsExist(String username) {
        System.out.println("2");
        int count=Integer.parseInt(accountMapper.usernameIsExist(username));
        if(count==1)
        {
            System.out.println("21111111111111111111111111111");
            return true;
        }
        else {
            System.out.println("200000000000000000000000000000000000");
            return false;
        }
    }
    public String passwordIsExist(String username){
        System.out.println("3111111111111111111111111111");
        return accountMapper.passwordIsExist(username);
    }
    public boolean usernameIsnotExist(String username) {
        System.out.println("2");
        int count=Integer.parseInt(accountMapper.usernameIsExist(username));
        if(count==0)
        {
            //System.out.println("21111111111111111111111111111");
            return true;
        }
        else {
            //System.out.println("200000000000000000000000000000000000");
            return false;
        }
    }

    public void insertLog(String title, String content, String username){
        accountMapper.insertLog(title,content,username);
        System.out.println("insert successfully");
    }


}
